// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Staircase Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 51 : TYPE_STAIRCASE

// Function declarations
reserve function Staircase_DebugDraw
reserve function Staircase_DebugSpawn

// Static Values

// Tables

function Staircase_DebugDraw
	DrawSprite(0)
end function


function Staircase_DebugSpawn
	CreateTempObject(TypeName[Staircase], 0, object.xpos, object.ypos)
	GetBit(temp0, object.direction, 0)
	if temp0 == 0
		object[tempObjectPos].direction = FLIP_NONE
	else
		object[tempObjectPos].direction = FLIP_X
	end if
end function


event ObjectMain
	object.priority = PRIORITY_ACTIVE
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 112, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.value0 = 32
				object.state++
			end if
		next
		break
	case 1
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 112, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		object.value0--
		if object.value0 < 0
			object.value0 = 128
			object.state++
		end if
		break
	case 2
		temp0 = object.xpos
		temp1 = object.ypos
		if object.direction == FLIP_NONE
			object.xpos += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if
		temp5 = object.value1
		temp2 = 1
		while temp2 < 5
			object.ypos += temp5
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					temp6 = object.ypos
					temp6 &= -0x10000
					temp7 = 0x4000
					temp7 *= temp2
					temp7 += object.ypos
					temp7 &= -0x10000
					temp7 -= temp6
					object[currentPlayer].ypos += temp7
				end if
				if object[currentPlayer].gravity == GRAVITY_GROUND
					if checkResult == 4
						CallFunction(PlayerObject_Kill)
					end if
				end if
			next
			object.xpos += temp4
			temp2++
		loop
		object.value1 += 0x4000
		object.xpos = temp0
		object.ypos = temp1
		object.value0--
		if object.value0 <= 0
			object.state++
		end if
		break
	case 3
		temp0 = object.xpos
		temp1 = object.ypos
		if object.direction == FLIP_NONE
			object.xpos += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if
		temp5 = object.value1
		temp2 = 1
		while temp2 < 5
			object.ypos += temp5
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if object[currentPlayer].gravity == GRAVITY_GROUND
					if checkResult == 4
						CallFunction(PlayerObject_Kill)
					end if
				end if
			next
			object.xpos += temp4
			temp2++
		loop
		object.xpos = temp0
		object.ypos = temp1
		break
	case 4
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 112, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 4
				object.value0 = 59
				object.state++
			end if
		next
		break
	case 5
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 112, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		object.value0--
		if object.value0 < 0
			object.value0 = 128
			object.state = 2
		end if
		break
	end switch
	if object.outOfBounds == 1
		object.priority = PRIORITY_ACTIVE_BOUNDS
		object.state = 0
		object.value1 = 0
		if object.propertyValue == 2
			object.state = 4
		end if
	end if
end event


event ObjectDraw
	switch object.state
	case 0
	case 1
	case 4
		DrawSprite(0)
		break
	case 2
	case 3
		temp2 = object.xpos
		temp3 = object.ypos
		if object.direction == FLIP_NONE
			temp2 += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if
		temp5 = object.value1
		temp3 += temp5
		DrawSpriteXY(1, temp2, temp3)
		temp2 += temp4
		temp3 += temp5
		DrawSpriteXY(1, temp2, temp3)
		temp2 += temp4
		temp3 += temp5
		DrawSpriteXY(1, temp2, temp3)
		temp2 += temp4
		temp3 += temp5
		DrawSpriteXY(1, temp2, temp3)
		break
	case 5
		temp2 = object.xpos
		if object.direction == FLIP_NONE
			temp2 += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if
		GetBit(temp5, object.value0, 2)
		temp5 <<= 16
		temp6 = 0
		while temp6 < 4
			temp3 = object.ypos
			temp3 += temp5
			temp5 ^= 0x10000
			DrawSpriteXY(1, temp2, temp3)
			temp2 += temp4
			temp6++
		loop
		break
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("SLZ/Objects.gif")
	SpriteFrame(-16, -16, 128, 32, 67, 26)
	SpriteFrame(-16, -16, 32, 32, 67, 26)
	foreach (TypeName[Staircase], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].propertyValue == 2
			object[arrayPos0].state = 4
		end if
	next
	SetTableValue(TypeName[Staircase], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Staircase_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Staircase_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
